<template>
  <div class="login-container">
    <div class="header">
      <div class="header-content">
        <img class="logo" src="@/assets/images/logo1.png" alt="树懒经纪人" />
        <div class="login">
          <a href="https://shulan.fun" target="_self">前往首页</a>
        </div>
      </div>
    </div>
    <div class="login-wrap">
      <div class="login-top">
        <div class="login-top-title">快速登录树懒电脑端</div>
        <div class="login-top-des">进入小程序 > 打开扫码按钮 > 扫码登录</div>
      </div>
      <div class="login-bottom">
        <div class="login-left">
          <img src="@/assets/images/login_new.png" alt />
          <!-- <p class="qrcode-tips">无需安装APP，微信扫码即可使用</p> -->
        </div>
        <div class="login-right">
          <img class="login-right-img" src="@/assets/images/login_2.png" alt="" />
          <h4 class="title">扫码登录</h4>
          <p class="tips">不是微信扫码登录哦～</p>
          <div class="qr-code-wrap">
            <img :src="code" alt="" style="width: 150px; height: 150px" />
            <!-- <vue-qr class="qr-code" :text="code" :margin="10" /> -->
            <div v-if="invalidCode" class="invalid-wrap">
              <span class="fresh" @click="freshCode">刷新</span>
            </div>
          </div>
          <!-- <p class="wel">欢迎使用树懒经纪人</p> -->
        </div>
      </div>
    </div>
    <h1 class="tip">
      房源采集软件哪个好？树懒经纪人支持扫码采集房源、图片去除实勘牌、合成视频、VR采集，数十万房产经纪人共同的选择
    </h1>
  </div>
</template>

<script>
// import VueQr from "vue-qr"; // 二维码生成
import { getUserToken, internalAuthorizationLogin, userLogin } from '@/api/user';
import { queryFormat } from '@/utils/utilsNew'

export default {
  name: 'Login',
  components: {
    // VueQr,
  },
  data() {
    return {
      loading: false,
      code: '', // 生成二维码code
      invalidCode: false, // 二维码失否失效
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    const querys = queryFormat(location.hash);
    if (querys.token) {
      internalAuthorizationLogin({ token: querys.token }).then((res) => {
        if (res.isSuccess && res.content) {
          this.$store
            .dispatch('userLogins', res)
            .then(() => {
              //setuserId API调用示例
              gio('setUserId', res.content.sessionUser.id);

              this.$router.push({
                path: this.redirect || '/'
              });
            })
            .catch(() => {});
        } else {
          this.$message.error('跳转登录失败，请刷新后重试！');
        }
      });
    } else {
      this.getUserToken();
    }
  },
  mounted() {},
  methods: {
    // 获取二维码code
    getUserToken() {
      getUserToken()
        .then(({ content }) => {
          let { token, qrCode } = content;
          this.code = 'data:image/jpg;base64,' + qrCode;
          this.getUserLogin(token);
        })
        .catch(() => {});
    },
    // 授权登录
    getUserLogin(code) {
      const that = this;
      userLogin({ code: code })
        .then((res) => {
          if (res.isSuccess) {
            that.$store
              .dispatch('userLogins', res)
              .then(() => {
                //setuserId API调用示例
                gio('setUserId', res.content.sessionUser.id);

                that.$router.push({
                  path: that.redirect || '/'
                });
              })
              .catch(() => {});
          } else {
            if (!res.content) {
              that.$message.error('请刷新后重新扫码！');
              that.invalidCode = true;
              return;
            }
            switch (res.content.status) {
              case 'init':
                setTimeout(() => {
                  that.getUserLogin(code);
                }, 1000);
                break;
              case 'invalid':
                that.invalidCode = true;
                break;
              default:
                break;
            }
          }
        })
        .catch(() => {});
    },
    // 刷新code
    freshCode() {
      this.invalidCode = false;
      this.getUserToken();
    }
  }
};
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  position: relative;
  background: url('~@/assets/images/login_bg.png') center center no-repeat;
  background-size: cover;

  .tip {
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 12px;
    color: #999;
  }

  .header {
    position: fixed;
    width: 100%;
    height: 60px;
    background-color: #fff;

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 0 auto;
      width: 1170px;
      height: 100%;
    }

    .logo {
      width: 143px;
      height: 38px;
    }

    .login {
      position: relative;
      padding: 0 24px;
      height: 30px;
      line-height: 30px;
      border-radius: 30px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      background: linear-gradient(270deg, #fc8b8a 0%, #f75352 100%);
      cursor: pointer;
    }

    .login::after {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      left: 10px;
      content: '';
      width: 0;
      height: 0;
      border-top: 5px solid transparent;
      border-right: 5px solid #fff;
      border-bottom: 5px solid transparent;
    }
  }
  .login-wrap {
    position: absolute;
    left: 50%;
    top: 56%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 30px 0 40px;
    border-radius: 6px;
    letter-spacing: 1px;
    box-sizing: border-box;
    box-shadow: 1px 1px 8px #ccc;
    background-color: #fff;

    .login-top {
      .login-top-title {
        color: #333;
        font-size: 40px;
      }

      .login-top-des {
        margin-top: 24px;
        font-size: 18px;
        text-align: center;
        color: #aaa;
      }
    }

    .login-bottom {
      display: flex;
      margin-top: 32px;
      padding: 0 50px 0 100px;
    }

    .login-left {
      padding-top: 24px;

      img {
        width: 350px;
        height: 300px;
        margin-right: 40px;
      }

      .qrcode-tips {
        text-align: left;
        margin-left: 20%;
      }
    }

    .login-right {
      position: relative;
      padding-top: 20px;
      width: 300px;
      height: 350px;
      text-align: center;
      letter-spacing: 2px;
      box-shadow: 0px 5px 50px 0px rgba(135, 135, 135, 0.17);
      box-sizing: border-box;

      .login-right-img {
        position: absolute;
        top: 20px;
        left: 60px;
        width: 44px;
        height: 62px;
      }

      .title {
        font-size: 26px;
        font-weight: normal;
        margin-bottom: 5%;
        color: #212121;
      }

      .tips {
        color: #808080;
        font-size: 15px;
        margin-bottom: 10%;
      }
      .qr-code-wrap {
        width: 50%;
        margin: 0 auto;
        position: relative;
        border-radius: 5px;
        overflow: hidden;

        .qr-code {
          width: 100%;
        }

        .invalid-wrap {
          position: absolute;
          left: 0;
          right: 0;
          top: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 0.6);
        }

        .fresh {
          display: inline-block;
          color: #fff;
          font-size: 14px;
          border: 1px #fff solid;
          border-radius: 15px;
          width: 80px;
          height: 30px;
          line-height: 30px;
          box-sizing: border-box;
          position: absolute;
          top: 50%;
          left: 50%;
          margin: -15px 0 0 -40px;
          cursor: pointer;
        }
      }

      .wel {
        margin-top: 15%;
        font-size: 15px;
        color: #808080;
      }
    }
  }
}
</style>
